package com.fan.shop.businesslogic;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.fan.shop.domain.Stock;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.StockService;
import com.fan.shop.services.StoreService;

public class StockUpdateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(StockUpdateProcess.class);

	private ItemService itemService;
	private StoreService storeService;
	private StockService stockService;

	private String itemCode;
	private String storeCode;
	private int qty;
	private BigDecimal netPrice;
	private BigDecimal cost;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockUpdateProcess.logger = logger;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public BigDecimal getNetPrice() {
		return netPrice;
	}

	public void setNetPrice(BigDecimal netPrice) {
		this.netPrice = netPrice;
	}

	public BigDecimal getCost() {
		return cost;
	}

	public void setCost(BigDecimal cost) {
		this.cost = cost;
	}

	@Override
	public Integer processImpl() {
		if (!stockService.hasStock(itemCode, storeCode)) {
			setErrorMessage("Item: " + itemCode + " doesn't exist in store: "
					+ storeCode + "!");
			return ShopGlobalVariables.FAILED;
		}
		if (!itemService.hasItem(itemCode)) {
			setErrorMessage("Item: " + itemCode + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (!storeService.hasStore(storeCode)) {
			setErrorMessage("Store: " + storeCode + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}

		Stock stock = stockService.getStock(itemCode, storeCode);

		double stock_cost = stock.getCost().doubleValue();
		double stock_price = stock.getNetPrice().doubleValue();
		int stock_qty = stock.getQuantity();

		stock_price = (stock_price * stock_qty + netPrice.doubleValue() * qty)
				/ (stock_qty + qty);
		stock_cost = (stock_cost * stock_qty + cost.doubleValue() * qty)
				/ (qty + stock_qty);
		stock_qty = stock_qty + qty;

		stock.setQuantity(stock_qty);
		stock.setCost(new BigDecimal(stock_cost));
		stock.setNetPrice(new BigDecimal(stock_price));
		
		stockService.updateStock(stock);
		return ShopGlobalVariables.SUCCESS;
	}

}
